package com.qasystem.qa.core.base;

import com.github.pagehelper.PageHelper;
import com.qasystem.qa.core.constants.Constants;
import com.qasystem.qa.core.utils.Reflections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.util.List;

public abstract class BaseService<T> implements Service<T> {
    @Autowired
    private Mapper<T> mapper;
    private Class<T> entityClass;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public T insert(T record) {
        mapper.insert(record);
        return record;
    }

    @Override
    public List<T> insert(List<T> recordList) {
        return null;
    }

    @Override
    public List<T> insertSelective(List<T> recordList) {
        return null;
    }

    @Override
    public T update(T record) {
        return null;
    }

    @Override
    public List<T> update(List<T> recordList) {
        return null;
    }

    @Override
    public List<T> updateSelective(T record) {
        return null;
    }

    @Override
    public List<T> updateSelective(List<T> recordList) {
        return null;
    }

    @Override
    public int delete(Long id) {
        return 0;
    }

    @Override
    public int delete(String ids) {
        return 0;
    }

    @Override
    public int delete(Long[] ids) {
        return 0;
    }

    @Override
    public int delete(T record) {
        return 0;
    }

    @Override
    public int delete(List<T> recordList) {
        return 0;
    }

    @Override
    public T persist(T record) {
        return null;
    }

    @Override
    public List<T> persist(List<T> recordList) {
        return null;
    }

    @Override
    public T persistSelective(T record) {
        return null;
    }

    @Override
    public List<T> persistSelective(List<T> recordList) {
        return null;
    }

    @Override
    public T get(String id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public T get(T record) {
        return mapper.selectOne(record);
    }

    @Override
    public T get(String key, Object value) {
        T entity = null;
        try {
            entity = entityClass.newInstance();
            Field field = Reflections.getField(entityClass, key);
            field.set(entity, value);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return mapper.selectOne(entity);
    }

    @Override
    public List<T> select(String ids) {
        return mapper.selectByIds(ids);
    }

    @Override
    public List<T> select(T record) {
        return mapper.select(record);
    }

    @Override
    public List<T> select(String key, Object value) {
        return null;
    }

    @Override
    public List<T> select(T record, int pageNum, int pageSize) {
        return null;
    }

    @Override
    public List<T> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public int count(T record) {
        return 0;
    }
}
